package com.springboot.ding;



import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;

/**
 * @description: 加签
 * @author: zyj
 * @create: 2020-07-23 10:36
 **/
public class DingSecret {

    public static String[] getSecret(String secret) throws Exception{
        Long timestamp = System.currentTimeMillis();
        System.out.println(timestamp);
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData,true)),"UTF-8");
        String [] signs = {String.valueOf(timestamp), sign};
        return signs;
    }

//    public static void main(String[] args) {
//        try {
//            System.out.println("加密："+DingSecret.getSecret("SEC4c4e806890f3ead1fb2fcb5e4b8d31aab5fb581fe2612c253991fb70c2f766c0"));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }

}
